/**
 * PlatformTransformer插件测试
 *
 * 此文件用于测试PlatformTransformer插件的效果
 * 展示插件如何在编译时移除非当前平台的代码
 */

const { PlatformTransformer } = require('./dist/index');
const babel = require('@babel/core');
const fs = require('fs');
const path = require('path');

// 确保安装了 @babel/preset-typescript
// npm install --save-dev @babel/preset-typescript

// 测试代码样本
const testSamples = [
  {
    name: '基础样式对象',
    code: `
function getButtonStyle() {
  return PlatformChain.with(() => ({
    color: 'black',
    fontSize: '14px'
  }))
  .web(() => ({
    color: 'blue',
    fontSize: '16px'
  }))
  .mpWeixin(() => ({
    color: 'green',
    fontSize: '32rpx'
  }))
  .app(() => ({
    color: 'red',
    fontSize: '18px'
  }))
  .exe();
}
`
  },
  {
    name: 'TypeScript类型支持',
    code: `
interface ButtonStyle {
  color: string;
  fontSize: string;
  transition?: string;
}

function getButtonStyle(): ButtonStyle {
  return PlatformChain.with<ButtonStyle>(() => ({
    color: 'black',
    fontSize: '14px'
  }))
  .web(() => ({
    color: 'blue',
    fontSize: '16px',
    transition: 'all 0.3s'
  }))
  .mpWeixin(() => ({
    color: 'green',
    fontSize: '32rpx'
  }))
  .app(() => ({
    color: 'red',
    fontSize: '18px'
  }))
  .exe();
}
`
  },
  {
    name: '函数调用链',
    code: `
function shareToPlatform(title, content, imageUrl) {
  return PlatformChain.with(() => {
    console.log('平台不支持分享');
    return false;
  })
  .web(() => {
    console.log('Web分享:', title, content);
    return true;
  })
  .mpWeixin(() => {
    console.log('微信分享:', title, content);
    return true;
  })
  .app(() => {
    console.log('App分享:', title, content);
    return true;
  })
  .exe();
}
`
  },
  {
    name: '复杂结构数据',
    code: `
function getComponentConfig() {
  return PlatformChain.with(() => ({
    name: 'DefaultComponent',
    props: { title: '默认标题' },
    events: { click: () => console.log('点击') }
  }))
  .web(() => ({
    name: 'WebComponent',
    props: { title: 'Web标题', responsive: true },
    events: { click: () => console.log('Web点击'), hover: () => console.log('悬停') }
  }))
  .mpWeixin(() => ({
    name: 'WechatComponent',
    props: { title: '微信标题', openType: 'share' },
    events: { click: () => console.log('微信点击'), getUserInfo: (info) => console.log(info) }
  }))
  .app(() => ({
    name: 'AppComponent',
    props: { title: 'App标题', nativeView: true },
    events: { click: () => console.log('App点击') }
  }))
  .exe();
}
`
  }
];

// 测试多个平台环境
const platforms = [
  { name: 'Web', codePlatform: 'WEB' },
  { name: '微信小程序', codePlatform: 'MP-WEIXIN' },
  { name: 'App', codePlatform: 'APP' }
];

// 创建输出目录
const outputDir = path.join(__dirname, 'transformer-test-output');
if (!fs.existsSync(outputDir)) {
  fs.mkdirSync(outputDir);
}

// 写入测试报告头部
const reportPath = path.join(outputDir, 'test-report.md');
fs.writeFileSync(reportPath, `# PlatformTransformer插件测试报告

本报告展示PlatformTransformer插件在不同平台环境下的代码转换效果。

`);

// 为每个测试样本创建一个测试文件
testSamples.forEach((sample, index) => {
  const samplePath = path.join(outputDir, `sample-${index + 1}.js`);
  fs.writeFileSync(samplePath, sample.code);

  // 添加样本信息到报告
  fs.appendFileSync(reportPath, `## 测试样本 ${index + 1}: ${sample.name}\n\n`);
  fs.appendFileSync(reportPath, '### 原始代码\n\n```javascript\n' + sample.code + '\n```\n\n');

  // 对每个平台进行测试
  platforms.forEach(platform => {
    // 创建Babel插件 - 使用选项参数
    const plugin = PlatformTransformer.createBabelPlugin(platform.codePlatform);

    try {
      // 转换代码
      const result = babel.transformSync(sample.code, {
        plugins: [plugin],
        presets: ['@babel/preset-typescript'],
        filename: samplePath,
        retainLines: true,
        parserOpts: {
          plugins: ['typescript']
        }
      });

      // 将转换结果添加到报告
      fs.appendFileSync(reportPath, `### ${platform.name}环境下的转换结果\n\n`);
      fs.appendFileSync(reportPath, '```javascript\n' + (result?.code || '转换失败') + '\n```\n\n');

      // 保存转换后的文件
      const outputPath = path.join(outputDir, `sample-${index + 1}-${platform.name}.js`);
      fs.writeFileSync(outputPath, result?.code || '转换失败');
    } catch (error) {
      // 记录错误信息
      fs.appendFileSync(reportPath, `### ${platform.name}环境下转换出错\n\n`);
      fs.appendFileSync(reportPath, '```\n' + error.stack + '\n```\n\n');

      console.error(`测试样本 ${index + 1} 在 ${platform.name} 环境下转换失败:`, error);
    }
  });

  fs.appendFileSync(reportPath, '---\n\n');
});

console.log(`测试完成！测试报告已生成：${reportPath}`);
